using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input.Touch;
namespace WindowsGame4
{
    public class Police : Enemy
    {
        public float chargeSpeed = 2000, minRange=200;
        public bool bIsCharging=false;
        public Vector2 power;
        public GameTimer chargeTimer;
        public Police()
            : base()
        {
            GetTexture();
            chargeTimer = new GameTimer(3);
            chargeTimer.bMultiTrigger = false;
            moveSpeed = 200;
            moveAcc = 100;
        }
        public void GetTexture()
        {
            int id = GameManager.random.Next(5);
            texture = GameManager.policeTextures[id];
            power = GameManager.policePowers[id];
        }
        public override void AI()
        {
            base.AI();
            Vector2 dir = GameManager.player.pos-pos;
            angle = GetAngle(dir) + 90;
            float distance = dir.Length();
            if (distance > 0)
                dir.Normalize();
            Charge();
        }
        public void Charge()
        {
            Vector2 dir = GameManager.player.pos - pos;
            float distance = dir.Length();
            if (distance > 0)
                dir.Normalize();
            if (distance > shootingRange && chargeTimer.IsTime())
            {
                vel = dir * chargeSpeed;
                bBlocks = false;
                bIsCharging = true;
            }
            else if (distance <= shootingRange)
            {
                if (bIsCharging)
                {
                    vel = GameManager.player.vel;
                    if (distance < minRange)
                        pos = GameManager.player.pos - dir * minRange;
                    bBlocks = true;
                    bIsCharging = false;
                }
                //clear the timer so that once the player exits the police range, the counter will start
                chargeTimer.IsTime();
            }

        }
        public override void React(GameObject obj, Vector2 cp)
        {
            base.React(obj, cp);
            if (bIsCharging && obj is Asteroid && !(obj is Planet))
                obj.bDead = true;
        }
        public override void Shoot()
        {
            Vector2 dir=GameManager.player.pos-pos;
            if(dir.Length()>0)
                dir.Normalize();
            Vector2 norm = new Vector2(dir.Y, -dir.X);
            for (int i = 0; i < power.X; i++)
            {
                Projectile p = new Projectile();
                p.hit = Projectile.HitType.Player;
                p.pos = pos+norm*power.Y*(i-2);
                p.color = new Vector4(0, 1, 0, 1);
                //p.scale = new Vector2(.5f);
                p.bBlocks = false;
                p.damage = 5;
                p.angle = GetAngle(dir);
                p.vel = dir * projectileSpeed + GameManager.player.vel;
                GameManager.AddObject(p);
            }
            GameManager.enemyShoot.Play();
        }
    }
}
